package com.ax.api.route.base.drugproducer;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.ax.api.route.util.RouteConfig;

@RestController
@RequestMapping("/drugClass")
public class DrugClassRoute {
	 @Autowired
	 LoadBalancerClient loadBalancerClient;
	 @Autowired
	 RestTemplate restTemplate;
	 
	 
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping("/sys/drugClass")
	String drugAdministration(Model model) {
	   String url = geturl("/sys/drugClass");
	   return restTemplate.getForObject(url, String.class);
	}
	
	/**
	 * queryDrugclass
	 * remote_base
	 * 药品分类查询(模块:药品分类)
	 */ 
	@GetMapping("/queryDrugclass")
	public String queryDrugclass(@RequestParam Map<String,Object> map) {
		String value = "?drugClassId={drugClassId}";
		String url = geturl("/sys/drugClass/queryDrugclass") + value;
		return restTemplate.getForObject(url, String.class,map);
}

	/**
	 * add
	 * remote_base
	 * 药品分类增加(模块:药品分类)*跳转增加页面
	 */   
	@GetMapping("/add/drugClass")
	public String add(String pId){
		String value = "/pId="+pId;
	    String url = geturl("/sys/drugClass/add") + value;
		return restTemplate.getForObject(url, String.class,pId);
	}
	
	/**
	 * insertBaseDrugClass
	 * remote_base
	 * 药品分类增加(模块:药品分类)
	 */ 
	@GetMapping("/insertBaseDrugClass")
	public String insertBaseDrugClass(@RequestParam Map<String,Object> map) {
		String value = "?drugClassId={drugClassId}&drugClassCode={drugClassCode}&drugClassName={drugClassName}&firstLetter={firstLetter}&parentClassId={parentClassId}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/drugClass/insertBaseDrugClass") + value;
		return restTemplate.getForObject(url, String.class,map);
  }
  
   /**
	 * edit
	 * remote_base
	 * 药品分类(模块:药品分类)*跳转修改页面
	 */   
	@GetMapping("/edit/drugClass")
	public String edit(String id){
		String value = "/id=" + id;
	    String url = geturl("/sys/drugClass/edit") + value;
		return restTemplate.getForObject(url, String.class,id);
	}
	
	/**
	 * updateBaseDrugClass
	 * remote_base
	 * 药品分类修改(模块:药品分类)
	 */ 
	@GetMapping("/updateBaseDrugClass")
	public String updateBaseDrugClass(@RequestParam Map<String,Object> map) {
		String value = "?drugClassId={drugClassId}&drugClassCode={drugClassCode}&drugClassName={drugClassName}&firstLetter={firstLetter}&parentClassId={parentClassId}&remarks={remarks}&flag={flag}";
		String url = geturl("/sys/drugClass/updateBaseDrugClass") + value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
	private String geturl(String path){
		 ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.BASE_ROUTE);
		 StringBuffer bf = new StringBuffer("http://");
		 bf.append(serviceInstance.getHost());
		 bf.append(":");
		 bf.append(serviceInstance.getPort());
		 bf.append(path);
		 return bf.toString();
	   }
}
